jQuery.fn.dataTableExt.oApi.fnPagingInfo = function ( oSettings )
		{
			return {
				"iStart":         oSettings._iDisplayStart,
				"iEnd":           oSettings.fnDisplayEnd(),
				"iLength":        oSettings._iDisplayLength,
				"iTotal":         oSettings.fnRecordsTotal(),
				"iFilteredTotal": oSettings.fnRecordsDisplay(),
				"iPage":          oSettings._iDisplayLength === -1 ?
					0 : Math.ceil( oSettings._iDisplayStart / oSettings._iDisplayLength ),
				"iTotalPages":    oSettings._iDisplayLength === -1 ?
					0 : Math.ceil( oSettings.fnRecordsDisplay() / oSettings._iDisplayLength )
			};
		};
	
	function dataTable(data,path) {
		var oLanguage = data.split(",");
		$("#roleMenuData").dataTable( {
			"oLanguage": {
	            "oPaginate": {
	                          "sFirst": oLanguage[0],// ปุ่มกลับมาหน้าแรก
	                          "sLast": oLanguage[1],//ปุ่มไปหน้าสุดท้าย
	                          "sNext": oLanguage[2], //ปุ่มหน้าถัดไป
	                          "sPrevious": oLanguage[3] // ปุ่ม กลับ
	                        	  },
	             "sLengthMenu": oLanguage[4],  
	             "sZeroRecords": oLanguage[5], 
	             "sInfo": oLanguage[6],
	             "sEmptyTable": oLanguage[7],
	             "sLoadingRecords": oLanguage[8],  
	             "sProcessing": oLanguage[9],
	             "sInfoEmpty": oLanguage[10],
	             "sInfoFiltered": oLanguage[11],  
	             "sSearch": oLanguage[12]
	        },
//			"fnRowCallback" : function(nRow, aData, iDisplayIndex){
//                $("td:first", nRow).html(iDisplayIndex +1);
//               return nRow;
//            },
	        "bProcessing": true,
	        "bFilter" : false,  
	        "bDestroy" : true,
//	        "bServerSide": true,
//	        "sort": "position",
	        //bStateSave variable you can use to save state on client cookies: set value "true" 
	        "bStateSave": false,
	        //Default: Page display length
	        "iDisplayLength": 10,
	        //We will use below variable to track page number on server side(For more information visit: http://legacy.datatables.net/usage/options#iDisplayStart)
	        "iDisplayStart": 0,
	        "fnDrawCallback": function () {
	            //Get page numer on client. Please note: number start from 0 So
	            //for the first page you will see 0 second page 1 third page 2...
	            //Un-comment below alert to see page number
	        	//alert("Current page number: "+this.fnPagingInfo().iPage);    
	        },       
	        "fnServerParams": function ( aoData ) { 
	        	  aoData.push( { "name": "vaUserId", "value": $("#vaRoleId").val() } );
	        	  aoData.push( { "name": "vaAppId", "value": $("#vaAppId").val() } );
	        	},
	        "sAjaxSource": "roleMenu/roleMenuDataTables",
	        "aoColumns": [	             
	      		{ "mData": null,"mRender": function ( data, type, full ) {    			
	      			var vaChecked = "";
					if($("#paApplicationId").val()!=""){
						var vaApplIdCheked = $("#paApplicationId").val().split(",");
						for(var i=0; i<vaApplIdCheked.length; i++){
							if(vaApplIdCheked[i] == (full.vaApplicationId)){
								vaChecked = "checked";
								break;
							}
						}
					}	
	      			return "<label class=\"label-checkbox\">"+
									" &nbsp;<input type='checkbox' class='fChkChoose"+full.vaMenuId+"'  id='chkChoose"+full.vaMenuId+"' name='chkChoose"+full.vaMenuId+"' onclick=\"enableCRUDC('chkChoose"+full.vaMenuId+"')\" value="+full.vaMenuId+" "+vaChecked+" >"+
									" <span class=\"custom-checkbox info\"></span>"+
							"</label>";			    				    
	      		},sClass: "center", bSortable: false, sWidth: '5%'  },
				{ "mData": "vaMenuId",sClass: "center", sWidth: '5%' },
				{ "mData": "vaMenuNameTh",sClass: "left",bSortable: false, sWidth: '19%'  },
				{ "mData": "vaMenuNameEn",sClass: "left", sWidth: '19%'  },
				{ "mData": "vaMenuUrl",sClass: "left", sWidth: '24%'  },
	      		{ "mData": null,"mRender": function ( data, type, full ) {    			
	      			var vaChecked = "";
					if($("#paApplicationId").val()!=""){
						var vaApplIdCheked = $("#paApplicationId").val().split(",");
						for(var i=0; i<vaApplIdCheked.length; i++){
							if(vaApplIdCheked[i] == (full.vaApplicationId)){
								vaChecked = "checked";
								break;
							}
						}
					}	
	      			return "<label class=\"label-checkbox\">"+
									" &nbsp;<input type='checkbox' class='chkChoose"+full.vaMenuId+"'  id='chkChoose"+full.vaMenuId+"'  name='chkChoose"+full.vaMenuId+"' onclick=\"getDataChecked('"+full.vaMenuId+"')\" value='C' DISABLED >"+
									" <span class=\"custom-checkbox info\"></span>"+
							"</label>";					    				    
	      		},sClass: "center", bSortable: false, sWidth: '7%'  },
	      		{ "mData": null,"mRender": function ( data, type, full ) {    			
	      			var vaChecked = "";
					if($("#paApplicationId").val()!=""){
						var vaApplIdCheked = $("#paApplicationId").val().split(",");
						for(var i=0; i<vaApplIdCheked.length; i++){
							if(vaApplIdCheked[i] == (full.vaApplicationId)){
								vaChecked = "checked";
								break;
							}
						}

					}	
	      			return "<label class=\"label-checkbox\">"+
									" &nbsp;<input type='checkbox' class='chkChoose"+full.vaMenuId+"'  id='chkChoose"+full.vaMenuId+"'  name='chkChoose"+full.vaMenuId+"' onclick=\"getDataChecked('"+full.vaMenuId+"')\" value='R' DISABLED >"+
									" <span class=\"custom-checkbox info\"></span>"+
							"</label>";					    				    
	      		},sClass: "center", bSortable: false, sWidth: '7%'  },
	      		{ "mData": null,"mRender": function ( data, type, full ) {    			
	      			var vaChecked = "";
					if($("#paApplicationId").val()!=""){
						var vaApplIdCheked = $("#paApplicationId").val().split(",");
						for(var i=0; i<vaApplIdCheked.length; i++){
							if(vaApplIdCheked[i] == (full.vaApplicationId)){
								vaChecked = "checked";
								break;
							}
						}

					}	
	      			return "<label class=\"label-checkbox\">"+
									" &nbsp;<input type='checkbox' class='chkChoose"+full.vaMenuId+"'  id='chkChoose"+full.vaMenuId+"'  name='chkChoose"+full.vaMenuId+"' onclick=\"getDataChecked('"+full.vaMenuId+"')\" value='U' DISABLED >"+
									" <span class=\"custom-checkbox info\"></span>"+
							"</label>";					    				    
	      		},sClass: "center", bSortable: false, sWidth: '7%'  },
	      		{ "mData": null,"mRender": function ( data, type, full ) {    			
	      			var vaChecked = "";
					if($("#paApplicationId").val()!=""){
						var vaApplIdCheked = $("#paApplicationId").val().split(",");
						for(var i=0; i<vaApplIdCheked.length; i++){
							if(vaApplIdCheked[i] == (full.vaApplicationId)){
								vaChecked = "checked";
								break;
							}
						}

					}	
	      			return "<label class=\"label-checkbox\">"+
									" &nbsp;<input type='checkbox' class='chkChoose"+full.vaMenuId+"'  id='chkChoose"+full.vaMenuId+"'  name='chkChoose"+full.vaMenuId+"' onclick=\"getDataChecked('"+full.vaMenuId+"')\" value='D' DISABLED >"+
									" <span class=\"custom-checkbox info\"></span>"+
							"</label>";	
	      		},sClass: "center", bSortable: false, sWidth: '7%'  },
	        ]} 
		);
		
		$('#chk-all').click(function()	{
			if($(this).is(':checked'))	{
				$('#roleAppdata').find('.chk-row').each(function()	{
					$(this).prop('checked', true);
					$(this).parent().parent().parent().addClass('selected');
				});
			}
			else	{
				$('#roleAppdata').find('.chk-row').each(function()	{
					$(this).prop('checked' , false);
					$(this).parent().parent().parent().removeClass('selected');
				});
			}
		});
	}
	
	var vaMenuIdLocal =  [];
	function getDataChecked(paMenuId) {
		// get checked data
		var crudChecked = "";
        $.each($("input[name='chkChoose"+paMenuId+"']:checked"), function(){            
//        	crudChecked.push($(this).val());
        	crudChecked += $(this).val();
        });
        
//        alert("data new checked :: " + crudChecked);
        var vbStillMenuId = false;
        var vaIndexData = 0;
        if ( vaMenuIdLocal != "" ){
//        	alert("if");
//        	alert("Length>>"+vaMenuIdLocal.length);
        	for ( i = 0; i < vaMenuIdLocal.length ;i++ ){
        		var tempMenuId = vaMenuIdLocal[i].substring(0,2);        		
        		//check still MenuId
        		if (tempMenuId == paMenuId){  
        			vaIndexData = i;
        			vbStillMenuId = true;
        		}
        	}
        	// check push new data or replace data
            if (vbStillMenuId){
//            	alert("still  id");
            	vaMenuIdLocal[vaIndexData] = crudChecked;  
            } else {
//            	alert("not still  id"); 
            	vaMenuIdLocal.push(crudChecked);
            }
        } else {
//        	alert("else");
        	vaMenuIdLocal.push(crudChecked);
        }
//        alert("vaMenuIdLocal :::" +vaMenuIdLocal);
//        for ( n = 0; n < vaMenuIdLocal.length; n++){
//        	 alert("Local data :: "+n+" >"+vaMenuIdLocal[n]); 
//        }
//        alert("Local data :: "+vaMenuIdLocal);     
        sortIdData();
	}
	
	$(document).ready(function() { 
//		document.getElementById('vaRoleId').readOnly  = false;
		var inputElements = document.getElementsByClassName('chkChoose');
	    $('#checkAll').click(function(event) { 
	        if(this.checked) { 
	            $('.chkChoose').each(function() { 
	                this.checked = true;  
	        		// get checked data
	        		var vaDataOnChecked = '' ;
	        		for(var i=0; inputElements[i]; ++i){
	        		      if(inputElements[i].checked){
	        		    	  vaDataOnChecked += inputElements[i].value+",";
	        		      }
	        		}		
	        		$("#paApplicationId").val(vaDataOnChecked);	                
	            });
	        }else{
	            $('.chkChoose').each(function() {
	                this.checked = false;                     
	            });         
	        }
	    });
	});

	function directToRoleMenu() {
		vaMenuIdLocal =  [];
		dataTable("","/jAuthen");
	}
	
	function enableCRUDC(paData) {	
        var vbStillMenuId = false;
        var vaIndexData = 0;
		if ( $("input."+paData+"").is(':disabled')){
			// enable CRUD
			$("input."+paData+"").attr("disabled", false);
		} else {
			var paTempId = paData.split("Choose");
			for ( i=0; i < vaMenuIdLocal.length;i++){
				var tempMenuId = vaMenuIdLocal[i].substring(0,2);       		
	    		if (tempMenuId == paTempId[1]){  
	    			vaIndexData = i;
	    			vbStillMenuId = true;
	    		}
			}
			if(vbStillMenuId){
				vaMenuIdLocal.splice(vaIndexData, 1);
			}
			// disable CRUD
			$("input."+paData+"").attr('checked', false);
			$("input."+paData+"").attr("disabled", true);
		}
//		alert(vaMenuIdLocal);
		sortIdData();
	}
	
	function sortIdData(){
		var resultData = "";
		for ( i=0; i < vaMenuIdLocal.length;i++){
			resultData += vaMenuIdLocal[i]+"/"		
		}
		$("#paMenuIdChecked").val(resultData);
	}